package com.smcdd.cdb.dbo.cdb_dbo.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.renderscript.Script;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.event.ShopType;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.YetLayMenPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IYetLayMenFg;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.MenDianActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.ShopMapActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 作者：Administrator on 2017/6/20 11:57
 * 邮箱：zhanghuaiha@gmail.com
 *  已铺设
 */

public class YetLayMenFragment extends BaseFragmentFragment<IYetLayMenFg,YetLayMenPresenter> implements IYetLayMenFg{
    private RecyclerView mRcycler;
    private EditText yet_search;
    private TextView yet_search_tv;
    private String type = "0";
    private ImageView yet_map;

    @Override
    public void initView(View rootView) {
        EventBus.getDefault().register(this);
        mRcycler = (RecyclerView) rootView.findViewById(R.id.yet_recycler);
        yet_search = (EditText) rootView.findViewById(R.id.yet_search);
        yet_search_tv = (TextView) rootView.findViewById(R.id.yet_search_tv);
        yet_map = (ImageView) rootView.findViewById(R.id.yet_map);
    }

    @Override
    public void initData() {
        mPresenter.loadContacts();
    }



    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void initListener() {
        yet_search_tv.setOnClickListener(v -> {
            String name = yet_search.getText().toString();
            mPresenter.search(name);
        });
        //地图
        yet_map.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), ShopMapActivity.class);
            startActivity(intent);
        });
//        yet_search_tv.setOnClickListener(v -> {
//            List<StoreInfo.DataBean> date = mPresenter.getDate(type);
//            mPresenter.searh(yet_search_tv.getText().toString(),date);
//            if (yet_search_tv.getText().toString().equals("搜索")) {
//                yet_search_tv.setText("取消");
//            }else {
//                yet_search_tv.setText("搜索");
//            }
//        });
//
        yet_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容

                } else {
                    // 此处为失去焦点时的处理内容
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                }
            }
        });

        mRcycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(recyclerView.getApplicationWindowToken(), 0);
            }
        });
    }




    @Override
    protected YetLayMenPresenter createPresenter() {
        return new YetLayMenPresenter((MenDianActivity)getActivity());
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_yet;
    }

    @Override
    public RecyclerView getRecycler() {
        return mRcycler;
    }

    @Override
    public MenDianActivity getMDActivity() {
        return (MenDianActivity)getActivity();
    }

    @Override
    public EditText getSearch() {
        return yet_search;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.unLoadData();
        EventBus.getDefault().unregister(this);
    }

    //刷新
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(String event){
        if (event.equals("update")) {
            mPresenter.loadData();
        }else if (event.equals("laid")) {
            mPresenter.loadData();
        }
    }

    //筛选
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSort(ShopType event){
        yet_search.setText("");
//        yet_search_tv.setText("搜索");
//        yet_search.setText("");
        Log.e("YetLayMenFragment",event.getType());
        if (event.getType().equals("6")) {
            mPresenter.loadData();
        }else {
            mPresenter.getDataInfo(event.getType());
            type = event.getType();
        }
    }
}
